package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.bean.ResultInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UIHelper;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.adapter.TeacherShowMineAdapter;
import com.ssyc.gsk_teacher.bean.TeacherShowMineInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 教师端--教师个人上传视频界面
 * Created by Kevin on 2018/6/29
 */

public class TeacherMineShowActivity extends BaseActivity implements View.OnClickListener {

    //返回键
    private ImageView ivBack;

    private RecyclerView rv;
    private RelativeLayout rlLoadingView;

    //空视图
    private View emptyView;

    //数据集合
    private List<TeacherShowMineInfo.ListBean> oldDatas;

    //适配器
    private TeacherShowMineAdapter adapter;

    //上传
    private ImageView iv_edit;

    //编辑
    private boolean edit = false;

    //全选
    private ImageView iv_allsel;

    //删除
    private ImageView iv_del;

    //上传
    private RelativeLayout tv_upload;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_show_mine;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //请求数据
        http();
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        oldDatas = new ArrayList<>();

        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);

        rv = findViewById(R.id.rv);
        rlLoadingView = findViewById(R.id.rl_loading_view);
        rlLoadingView.setVisibility(View.VISIBLE);

        iv_edit = findViewById(R.id.iv_edit);
        iv_allsel = findViewById(R.id.iv_allsel);
        iv_del = findViewById(R.id.iv_del);
        tv_upload = findViewById(R.id.tv_upload);

        iv_edit.setOnClickListener(this);
        iv_allsel.setOnClickListener(this);
        iv_del.setOnClickListener(this);
        tv_upload.setOnClickListener(this);


        if (!"2".equals(AccountUtils.getRole(this))) {
            iv_edit.setVisibility(View.GONE);
        }
    }

    /**
     * 模拟数据
     */
    private void http() {

        Map<String, String> map = new HashMap<>();
        map.put("type", "14");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");
        map.put("apptoken", AccountUtils.getToken(this));

        HttpUtils.post(HttpAdress.TEACHERSHOWMINE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    TeacherShowMineInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TeacherShowMineInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }

                    if (null != info) {
                        if ("200".equals(info.getState())) {

                            List<TeacherShowMineInfo.ListBean> newDatas = info.getList();
                            Log.i("test", "个人视频数据是：" + newDatas.size());
                            Log.i("test", "个人视频数据是：" + response);
                            oldDatas.clear();
                            oldDatas.addAll(newDatas);
                            initRv();

                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }

    /**
     * 初始化列表控件
     */
    private void initRv() {

        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        adapter = new TeacherShowMineAdapter(this, R.layout.teacher_rv_show_mine, oldDatas);
        BqaManager.setRv(emptyView, this, adapter, rv, null);

    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.iv_edit) {
            //编辑
            if (edit) {
                //退出编辑
                adapter.Edit(false);
                edit = false;
                iv_allsel.setVisibility(View.GONE);
                iv_del.setVisibility(View.GONE);
            } else {
                //进入编辑
                adapter.Edit(true);
                edit = true;
                iv_allsel.setVisibility(View.VISIBLE);
                iv_del.setVisibility(View.VISIBLE);
            }
        } else if (id == R.id.iv_allsel) {
            //全选
            adapter.selectALL(true);
        } else if (id == R.id.iv_del) {
            //删除
            String json = adapter.getJson();
            adapter.Edit(false);
            edit = false;
            iv_allsel.setVisibility(View.GONE);
            iv_del.setVisibility(View.GONE);
            if (!"[]".equals(json)) {
                delHttp(json);
            }
        } else if (id == R.id.tv_upload) {

            Intent intent = new Intent(this, TeacherUploadVidioActivity.class);
            startActivity(intent);

        }
    }


    /**
     * 删除选中的视频
     */
    private void delHttp(String json) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "15");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("rowIds", json);

        HttpUtils.post(HttpAdress.TEACHERSHOWMINE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {


                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }

                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            UIHelper.showToast(TeacherMineShowActivity.this, "删除成功");
                            http();

                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }
}
